﻿using System;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Domain;

namespace Fuse8.ContentManagementFramework.Domain.Bases
{
    [Serializable]
    public abstract class CmsContentBasedEntity : CmsTrackingEntityObject, IPublishedEntityObject
    {
        public virtual string DateFormat 
        {
            get
            {
                return "dd/MM/yyyy";
            }
        }
        
        public virtual string UserCreatedBy { get; set; }

        public virtual string PublicationDates
        {
            get
            {
                string result = DatePublished.ToString(DateFormat);

                if (PublishEndDate.HasValue)
                {
                    result = string.Format("{0} - {1}", result, PublishEndDate.Value.ToString(DateFormat));
                }

                return result;
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public bool IsCurrentlyPublished
        {
            get
            {
                var currentDate = DateTime.Now;

                return IsPublished && ((currentDate >= DatePublished) && (currentDate <= PublishEndDate.GetValueOrDefault(DateTime.MaxValue)));
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public DateTime DatePublished
        {
            get
            {
                return PublishStartDate.HasValue ? PublishStartDate.Value : DateCreated;
            }
        }

        #region IPublishedEntityObject Members

        public virtual DateTime? PublishStartDate { get; set; }

        public virtual DateTime? PublishEndDate { get; set; }

        public virtual bool IsPublished { get; set; }

        #endregion
                
        public abstract string ContentType { get; }
    }
}
